﻿package org.xhs.report.common.adv
{
    import baidu.wenku.common.utils.*;
    import flash.events.*;
    import flash.net.*;

    public class AdvRdModel extends EventDispatcher
    {
        private var dataLoader:URLLoader;
        private var _advUrl:String;
        private var _advLink:String;
        private var _advType:String;
        private var _advWidth:Number;
        private var _advHeight:Number;
        private var _advDataUrl:String;
        public static const ADV_COMPLETE:String = "ADV_COMPLETE";
        public static const ADV_ERROR:String = "ADV_ERROR";

        public function AdvRdModel(param1:IEventDispatcher = null)
        {
            super(param1);
            return;
        }// end function

        private function loaderComplete(event:Event) : void
        {
            var xml:XML;
            var e:* = event;
            this.dataLoader.removeEventListener(Event.COMPLETE, this.loaderComplete);
            this.dataLoader.removeEventListener(IOErrorEvent.IO_ERROR, this.loaderError);
            this.dataLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.loaderError);
            var data:* = this.dataLoader.data;
            var index1:* = data.indexOf("<");
            var index2:* = (data.lastIndexOf(">") + 1);
            try
            {
                xml = new XML(data.substring(index1, index2));
                this._advUrl = xml.@src;
                this._advLink = xml.@link;
                this._advType = xml.@type;
                this._advWidth = Number(xml.@width);
                this._advHeight = Number(xml.@height);
                if (this._advUrl)
                {
                    dispatchEvent(new Event(AdvRdModel.ADV_COMPLETE));
                }
                else
                {
                    dispatchEvent(new Event(AdvRdModel.ADV_ERROR));
                }
            }
            catch (e:Error)
            {
                dispatchEvent(new Event(AdvRdModel.ADV_ERROR));
            }
            return;
        }// end function

        private function loaderError(event:Event = null) : void
        {
            Logger.error("failed to load adv." + event);
            this.dataLoader.removeEventListener(Event.COMPLETE, this.loaderComplete);
            this.dataLoader.removeEventListener(IOErrorEvent.IO_ERROR, this.loaderError);
            this.dataLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.loaderError);
            dispatchEvent(new Event(AdvRdModel.ADV_ERROR));
            return;
        }// end function

        public function init() : void
        {
            if (!this._advDataUrl)
            {
                return;
            }
            this.dataLoader = new URLLoader();
            this.dataLoader.addEventListener(Event.COMPLETE, this.loaderComplete);
            this.dataLoader.addEventListener(IOErrorEvent.IO_ERROR, this.loaderError);
            this.dataLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.loaderError);
            this.dataLoader.load(new URLRequest(this._advDataUrl));
            return;
        }// end function

        public function get advUrl() : String
        {
            return this._advUrl;
        }// end function

        public function get advLink() : String
        {
            return this._advLink;
        }// end function

        public function get advType() : String
        {
            return this._advType;
        }// end function

        public function get advWidth() : Number
        {
            return this._advWidth;
        }// end function

        public function get advHeight() : Number
        {
            return this._advHeight;
        }// end function

        public function set advHeight(param1:Number) : void
        {
            this._advHeight = param1;
            return;
        }// end function

        public function get advDataUrl() : String
        {
            return this._advDataUrl;
        }// end function

        public function set advDataUrl(param1:String) : void
        {
            this._advDataUrl = param1;
            return;
        }// end function

    }
}
